package com.example.basic.web.service.stretcher.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.basic.base.impl.BaseEntityServiceImpl;
import com.example.basic.exception.ServiceException;
import com.example.basic.utils.IdUtils;
import com.example.basic.utils.StringUtils;
import com.example.basic.web.enums.DeviceStatusEnum;
import com.example.basic.web.enums.YesNoEnum;
import com.example.basic.web.mapper.stretcher.StretcherMapper;
import com.example.basic.web.model.stretcher.Stretcher;
import com.example.basic.web.service.stretcher.StretcherService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import static com.example.basic.utils.CheckUtil.notNull;


@Service
public class StretcherServiceImpl extends BaseEntityServiceImpl<StretcherMapper, Stretcher> implements StretcherService {

    @Resource
    private StretcherMapper stretcherMapper;

    /**
     * 新增
     * @param layerNum
     * @param columnNum
     * @return
     */
    @Override
    public Boolean add(Integer layerNum, Integer columnNum) {
        notNull(layerNum,"层数不能为空");
        notNull(columnNum,"列数不能为空");

        List<Stretcher> saveList = new ArrayList<>();

        //根据层数，列数批量生成库位
        for (Integer layer = 1; layer <= layerNum; layer++) {
            for (Integer column = 1; column <= columnNum; column++) {
                Stretcher stretcher = new Stretcher();
                stretcher.setColumnNum(column);
                stretcher.setLayerNum(layer);
                stretcher.setIsUse("0");
                stretcher.setId(IdUtils.fastUUID());
                preCreate(stretcher);
                saveList.add(stretcher);
            }
        }

        return saveBatch(saveList);
    }

    /**
     * 担架板库位列表
     * @param queryPage
     * @param isUse
     * @return
     */
    @Override
    public IPage<Stretcher> queryList(Page<Stretcher> queryPage, String isUse) {
        LambdaQueryWrapper<Stretcher> lqw = new LambdaQueryWrapper<>();
        if(StringUtils.notEmptyParams(isUse)){
            lqw.eq(Stretcher::getIsUse,isUse);
        }
        lqw.orderByAsc(Stretcher::getLayerNum);
        lqw.orderByAsc(Stretcher::getColumnNum);

        return this.mapper.selectPage(queryPage,lqw);
    }

    /**
     * 获取已占用的担架板库位
     * @return
     */
    @Override
    public Stretcher queryUseDepot() {
        LambdaQueryWrapper<Stretcher> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Stretcher::getIsUse,"1");
        lqw.orderByAsc(Stretcher::getColumnNum);
        lqw.orderByAsc(Stretcher::getLayerNum);
        lqw.last("limit 1");

        return this.mapper.selectOne(lqw);
    }

    /**
     * 获取未占用的担架板库位
     * @return
     */
    @Override
    public Stretcher queryUnUseDepot() {
        LambdaQueryWrapper<Stretcher> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Stretcher::getIsUse,"0");
        lqw.orderByAsc(Stretcher::getColumnNum);
        lqw.orderByAsc(Stretcher::getLayerNum);
        lqw.last("limit 1");

        return this.mapper.selectOne(lqw);
    }

    /**
     * 更新库位状态
     * @param id
     * @return
     */
    @Override
    public Boolean freeOrUse(String id) {
        notNull(id,"唯一id不能为空");

        Stretcher stretcher = this.mapper.selectById(id);

        if(stretcher == null){
            throw new ServiceException("担架板库位不存在");
        }

        //停用启用
        if(stretcher.getIsUse().equals(YesNoEnum.YES_FLAG.getType())){
            stretcher.setIsUse(YesNoEnum.NO_FLAG.getType());
        }else {
            stretcher.setIsUse(YesNoEnum.YES_FLAG.getType());
        }

        preUpdate(stretcher);

        this.mapper.updateById(stretcher);

        return true;
    }

    /**
     * 修改库位点位
     * @param id
     * @param point
     * @return
     */
    @Override
    public Boolean modify(String id, String point) {
        notNull(id, "id不能为空");
        notNull(point, "库位点位不能为空");

        Stretcher stretcher = this.selectById(id);
        if(stretcher == null){
            throw new ServiceException("未获取到需要修改的库位信息");
        }

        //判断点位是否已维护
        LambdaQueryWrapper<Stretcher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Stretcher::getStretcherPoint, point);
        queryWrapper.ne(Stretcher::getId, id);
        List<Stretcher> stretchers = this.mapper.selectList(queryWrapper);
        if(!stretchers.isEmpty()){
            throw new ServiceException("库位点位已存在");
        }

        stretcher.setStretcherPoint(point);
        preUpdate(stretcher);

        this.updateById(stretcher);

        return true;
    }
}
